﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using socketlib_basic;

namespace sendfile_client_gui
{
    public partial class Form_FileTransfer : Form
    {
        public Form_FileTransfer()
        {
            InitializeComponent();
        }

        private void button_OpenFile_Click(object sender, EventArgs e)
        {
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK) // Test result.
            {
                string file = openFileDialog1.FileName;
                textBox_filepath.Text = file;
            }

        }

        // connection server and send file
        private void button_SendFIle_Click(object sender, EventArgs e)
        {
            // connect to server 
            string _serverip = null;
            int _port;
            sendfile_client client = null; 
            _serverip = textBox_ServerIP.Text;
            _port = Int32.Parse(textBox_ServerPort.Text);
            if (_serverip == "" || _port <= 0)
            {
                toolStripStatusLabel1.Text = "Serverip ip, port incorrect";
                return;
            }

            client = new sendfile_client(_serverip, _port);
            if (!client.Connected)
            {
                toolStripStatusLabel1.Text = "Connect to Server/IP Fail";
                return;
            }

            int ret = client.SendFile(textBox_filepath.Text) ;
            if (ret== 1)
            {
                toolStripStatusLabel1.Text = "transfer succeed!";
            }
            else if (ret <= 0)
            {
                toolStripStatusLabel1.Text = "transfer fail!";
            }
            
        }

        private void Form_FileTransfer_Load(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text = "test";
        }

    }
}
